﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
#if (WINRT)
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
#else
using System.Windows.Markup;
#endif

namespace nRoute.Components
{
#if (WINRT)
    [ContentProperty(Name="Value")]
#else
    [ContentProperty("Value")]
#endif
    public partial class DependencyParameter
#if (SILVERLIGHT)
        : DependencyObject,
#elif (WINRT)
        : FrameworkElement,
#else
        : Freezable, 
#endif
        IEquatable<DependencyParameter>
    {

#region Declarations

        private const string KEY_SETONCE_ONLY = "BindableParameter's key can only set once";

        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.Register("Key", typeof(string), typeof(DependencyParameter),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, new PropertyChangedCallback(OnKeyChanged)));

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(Object), typeof(DependencyParameter),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

#endregion

        public DependencyParameter() { }

        public DependencyParameter(string key, Object value)
        {
            this.Key = key;
            this.Value = value;
        }

#region Properties

        [Category("Common Properties")]
        public string Key
        {
            get { return Convert.ToString(GetValue(KeyProperty)); }
            set { SetValue(KeyProperty, value); }
        }

        [Category("Common Properties")]
//#if WP7
//        [TypeConverter(typeof(nRoute.Components.TypeConverters.ConvertFromStringConverter))]
//#endif
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Object Value
        {
            get { return GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

#endregion

#region Handlers

        private static void OnKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(e.OldValue)))
            {
                throw new ArgumentException(KEY_SETONCE_ONLY, "Key");
            }
        }
        
#endregion

#if (!SILVERLIGHT && !WINRT)

#region Overrides

        protected override Freezable CreateInstanceCore()
        {
            return new DependencyParameter();
        }

#endregion

#endif

#region IEquatable<DependencyParameter> Members

        public bool Equals(DependencyParameter other)
        {
            if (other == null) return false;
            return string.Equals(Key, other.Key, Utilities.Compatability.INVARIANT_IGNORECASE_STRING_COMPARISION);
        }

#endregion

    }
}